﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Data.Inspection;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using VMind.Global;

namespace PPI.ServiceLogic.Inspections
{
    public class LInspection
    {

        public static List<EInspection> SelectAll(string connectionKey)
        {
            DataSet dsInspections = DInspection.SelectAll(Int32.MinValue, connectionKey);
            return ParseAllDatatableInspections(connectionKey, dsInspections.Tables[0]);
        }

        public static EInspection GetById(string connectionKey, Int32 id)
        {
            DataTable _dt = DInspection.SelectAll(id, connectionKey).Tables[0];
            return ParseDatatableInspection(id, connectionKey, _dt);
        }

        public static DataSet GetInspectionById(Int32 inspectionId, string connectionKey)
        {
            return DInspection.SelectAll(inspectionId, connectionKey);
        }

        private static EInspection ParseDatatableInspection(int id, string connectionKey, DataTable dtInspection)
        {
            DataRow _dr = dtInspection.Rows[0];
            EInspection _inspection = new EInspection();
            _inspection.Id = ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XMissionStructure_PoleId"]);
            _inspection.AcquisitionDate = ConvertHelper.ToDateTime(_dr["AcquisitionDate"]);
            _inspection.Class = new EXMissionStructures_PoleClass(ConvertHelper.ToInt32(_dr["ClassFK"]), ConvertHelper.Tostring(_dr["Class_Name"]));
            _inspection.Company = new ECompany(ConvertHelper.ToInt32(_dr["CompanyFk"]));
            _inspection.Inspector = new EInspector(ConvertHelper.ToInt32(_dr["InspectorFk"]));
            _inspection.Latitude = ConvertHelper.Todouble(_dr["Latitude"]);
            _inspection.Longitude = ConvertHelper.Todouble(_dr["Longitude"]);
            _inspection.Length = ConvertHelper.ToInt32(_dr["Length"]);
            _inspection.LastVisit = ConvertHelper.ToDateTime(_dr["LVisit"]);
            _inspection.Map = new EMap(ConvertHelper.ToInt32(_dr["MapFk"]));
            _inspection.Section = new ESection(ConvertHelper.ToInt32(_dr["SectionFk"]));
            _inspection.Number = ConvertHelper.Tostring(_dr["Number"]);
            _inspection.Specie = new EXMissionStructures_PoleSpecies(ConvertHelper.ToInt32(_dr["SpecieFk"]));
            _inspection.TopType = new EXMissionStructures_TopType(ConvertHelper.ToInt32(_dr["ToptypeFk"]));
            _inspection.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.ToInt32(_dr["TreatmentFk"]));
            _inspection.Year = ConvertHelper.ToInt32(_dr["Year"]);
            _inspection.Project = new EVendor_Project(ConvertHelper.ToInt32(_dr["Vendor_ProjectFk"]));
            _inspection.Contact = new EVendor_Contact(ConvertHelper.ToInt32(_dr["Vendor_ContactFk"]));
            _inspection.Structure = new EInspectionStructure(ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XMissionStructureFk"]));
            _inspection.Images = LInspectionImage.GetByInspection(connectionKey, id);
            
            return _inspection;
        }

        public static Int32 Save(Int32 inspectionId, double latitude, double longitude, Int32 year, DateTime lVisit, string number, DateTime acquisitionDate, Int32 treatmentFk, Int32 classFk, Int32 SpecieFk, Int32 ToptypeFk, Int32 length, Int32 inspectorFk, Int32 companyFk, Int32 mapFk, Int32 projectFk, Int32 contactFk, Int32 assetFk, Int32 sectionFk, Int32 guardsQuantity, bool poleTag, bool billable, bool groundWireRepair, string lastCompany, int visitTypeFk, string connectionKey)
        {
            Int32 returnId = inspectionId;

            if (inspectionId == Int32.MinValue)
                returnId = Insert(latitude, longitude, year, lVisit, number, acquisitionDate, treatmentFk, classFk, SpecieFk, ToptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, sectionFk, guardsQuantity, poleTag, billable, groundWireRepair, lastCompany, visitTypeFk, connectionKey);
            else
                Update(inspectionId, latitude, longitude, year, lVisit, number, acquisitionDate, treatmentFk, classFk, SpecieFk, ToptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, assetFk, sectionFk, guardsQuantity, poleTag, billable, groundWireRepair, lastCompany, visitTypeFk, connectionKey);
            
            return returnId;
        }

        private static int Insert(double latitude, double longitude, int year, DateTime visit, string number, DateTime acquisitiondate, int treatmentfk, int classFk, int specieFk, int toptypeFk, int length, int inspectorFk, int companyFk, int mapFk, Int32 projectFk, Int32 contactFk, Int32 sectionFk, Int32 guardsQuantity, bool poleTag, bool billable, bool groundWireRepair, string lastCompany, int visitTypeFk, string connectionKey)
        {
            return
                DInspection.Insert(latitude, longitude, year, visit, number, acquisitiondate, treatmentfk, classFk,
                                   specieFk, toptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, sectionFk, guardsQuantity, poleTag, billable, groundWireRepair, lastCompany, visitTypeFk, connectionKey);
        }

        private static void Update(Int32 inspectionId, double latitude, double longitude, int year, DateTime visit, string number, DateTime acquisitiondate, int treatmentfk, int classFk, int specieFk, int toptypeFk, int length, int inspectorFk, int companyFk, int mapFk, Int32 projectFk, Int32 contactFk, Int32 assetFk, Int32 sectionFk, Int32 guardsQuantity, bool poleTag, bool billable, bool groundWireRepair, string lastCompany, int visitTypeFk, string connectionKey)
        {
                DInspection.Update(inspectionId,latitude, longitude, year, visit, number, acquisitiondate, treatmentfk, classFk,
                                   specieFk, toptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, assetFk, sectionFk, guardsQuantity, poleTag, billable, groundWireRepair, lastCompany, visitTypeFk, connectionKey);
        }

        public static void SetVisit(Int32 inspectionId, Int32 visitId, string connectionKey)
        {
            DInspection.SetVisit(inspectionId,visitId,connectionKey);
        }

        public static Int32 NewVisitByPole(Int32 inspectionId, List<Int32> inspectionList,  string connectionKey)
        {
            Int32 visitId =DInspection.CreateVisitByPole(inspectionId, connectionKey);
            //throw new Exception("Visit Id -> "+visitId);

            foreach (int i in inspectionList)
            {
                DInspection.SetVisit(i,visitId,connectionKey);
            }

            return visitId;
        }

        public static Int32 InsertRemedy(Int32 inspectionPoleFk, DateTime discoveredDate, Int32 inspectionResultTypeFk, string connectionKey)
        {
            return DInspection.InsertRemedy(inspectionPoleFk, discoveredDate, inspectionResultTypeFk,connectionKey);
        }

        public static void UpdateRemedy(Int32 remedyId, Int32 remedyStatusFk, string remedyAssigned, DateTime remedyLastAction, DateTime remedyResolvedDate, string remedyNotes, string connectionKey)
        {
            DInspection.UpdateRemedy(remedyId,remedyStatusFk,remedyAssigned,remedyLastAction,remedyResolvedDate,remedyNotes,connectionKey);
        }

        private static List<EInspection> ParseAllDatatableInspections(string connectionKey, DataTable dtInspection)
        {
            List<EInspection> listInspections = new List<EInspection>();

            List<EInspectionImage> listImages = LInspectionImage.GetByInspection(connectionKey, Int32.MinValue);
            List<ENote> listNotes = LNote.GetByResultId(connectionKey, Int32.MinValue);
            foreach (DataRow _dr in dtInspection.Rows)
            {
                EInspection _inspection = new EInspection();
                _inspection.Id = ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XMissionStructure_PoleId"]);
                _inspection.AcquisitionDate = ConvertHelper.ToDateTime(_dr["AcquisitionDate"]);
                _inspection.Class = new EXMissionStructures_PoleClass(ConvertHelper.ToInt32(_dr["ClassFK"]), ConvertHelper.Tostring(_dr["Class_Name"]));
                _inspection.Company = new ECompany(ConvertHelper.ToInt32(_dr["CompanyFk"]));
                _inspection.Inspector = new EInspector(ConvertHelper.ToInt32(_dr["InspectorFk"]));
                _inspection.Latitude = ConvertHelper.Todouble(_dr["Latitude"]);
                _inspection.Longitude = ConvertHelper.Todouble(_dr["Longitude"]);
                _inspection.Length = ConvertHelper.ToInt32(_dr["Length"]);
                _inspection.LastVisit = ConvertHelper.ToDateTime(_dr["LVisit"]);
                _inspection.Map = new EMap(ConvertHelper.ToInt32(_dr["MapFk"]));
                _inspection.Section = new ESection(ConvertHelper.ToInt32(_dr["SectionFk"]));
                _inspection.Number = ConvertHelper.Tostring(_dr["Number"]);
                _inspection.Specie = new EXMissionStructures_PoleSpecies(ConvertHelper.ToInt32(_dr["SpecieFk"]));
                _inspection.TopType = new EXMissionStructures_TopType(ConvertHelper.ToInt32(_dr["ToptypeFk"]));
                _inspection.Treatment = new EXMissionStructures_PoleTreatment(ConvertHelper.ToInt32(_dr["TreatmentFk"]));
                _inspection.Year = ConvertHelper.ToInt32(_dr["Year"]);
                _inspection.VendorVisitType = new EVendor_Visit_Type(ConvertHelper.ToInt32(_dr["Vendor_Visit_TypeFk"]), ConvertHelper.Tostring(_dr["VendorVisitType_Name"]));
                _inspection.Project = new EVendor_Project(ConvertHelper.ToInt32(_dr["Vendor_ProjectFk"]));
                _inspection.Contact = new EVendor_Contact(ConvertHelper.ToInt32(_dr["Vendor_ContactFk"]));
                _inspection.GuardsQuantity = ConvertHelper.ToInt32(_dr["GuardsQuantity"]);
                _inspection.Billable = _dr["Billable"] == DBNull.Value ? false : (bool) _dr["Billable"];
                _inspection.PoleTag = _dr["PoleTag"] == DBNull.Value ? false : (bool)_dr["PoleTag"];
                _inspection.GroundWireRepair = _dr["GroundWireRepair"] == DBNull.Value ? false : (bool)_dr["GroundWireRepair"];
                _inspection.LastCompanyName = ConvertHelper.Tostring(_dr["LInspector"]);
                _inspection.Structure = new EInspectionStructure(ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XMissionStructureFk"]));

                _inspection.Images = listImages.FindAll(delegate(EInspectionImage i) { return i.Inspection.Id == _inspection.Id; }); 

                _inspection.Result = new EInspectionResult();
                _inspection.Result.Id = ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XmissionStructures_ResultId"]);
                _inspection.Result.InspectionType = new EInspectionTypes(ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XmissionStructures_Inspection_TypeFk"]), ConvertHelper.Tostring(_dr["Type_Name"]));
                _inspection.Result.LastInspection = ConvertHelper.Tostring(_dr["LastInspection"]);
                _inspection.Result.Resultype = new EResultType(ConvertHelper.ToInt32(_dr["Vendor_InspVisit_XmissionStructures_ResultTypeFk"]), ConvertHelper.Tostring(_dr["Result_Name"]));
                _inspection.Result.Notes =listNotes.FindAll(delegate(ENote n) { return n.InspectionResult.Id == _inspection.Result.Id; });
                _inspection.Result.IsElectrical = ConvertHelper.Tobool(_dr["bElectrical"]);
                _inspection.Result.Comments = _dr["Comments"].ToString();
                _inspection.Result.Inspection = _inspection;

                listInspections.Add(_inspection);

            }
            return listInspections;
        }
    }
}
